

package com.hazelcast.internal.util.phonehome;

import com.hazelcast.client.config.RoutingMode;
import com.hazelcast.client.impl.ClientEndpoint;
import com.hazelcast.instance.impl.Node;

import java.util.EnumMap;
import java.util.Map;

import static com.hazelcast.client.config.RoutingMode.*;

public class ClientRoutingModeMetricsProvider implements MetricsProvider {

    @Override
    public void provideMetrics(Node node, MetricsCollectionContext context) {
        Map<RoutingMode, Integer> routingModeCounts = new EnumMap<>(RoutingMode.class);


        for (ClientEndpoint client : node.getClientEngine().getEndpointManager().getEndpoints()) {
            RoutingMode mode = client.getRoutingMode();
            if (mode.isKnown()) {
                routingModeCounts.put(mode, routingModeCounts.getOrDefault(mode, 0) + 1);
            }
        }

        context.collect(PhoneHomeMetrics.ALL_MEMBERS_CLIENTS_COUNT, routingModeCounts.getOrDefault(ALL_MEMBERS, 0));
        context.collect(PhoneHomeMetrics.SINGLE_MEMBER_CLIENTS_COUNT, routingModeCounts.getOrDefault(SINGLE_MEMBER, 0));
        context.collect(PhoneHomeMetrics.MULTI_MEMBER_CLIENTS_COUNT, routingModeCounts.getOrDefault(MULTI_MEMBER, 0));
    }
}
